(function(){
	       var obj={};
	       //用来设置ajax回调函数的上下文。让回调函数中的this指向这个对象。
	       obj.ajax = function(type, url, data, headers, context, callback){
            $.ajax({
                 type: type,
			         url: url,
			         data: JSON.stringify(data),
			         headers: headers,
			         context:context
		          }).done(callback);
	           };
	       window.ajaxRequest = obj;
     })();
     
     function orderListShow(template){
         this.orderTemplate = template;
         this.orderModel = null;
         this.findOrderByStatus = function(){
             /*添加查询操作,根据spring提供的借口做出修改*/
             ajaxRequest.ajax("GET","/spring-merchant/orders/status?order_status_id=SUCCESS",null,null,this,function(orders){
			        this.orderModel = orders;
			        _render_order(this.orderTemplate, this.orderModel);
		     });
         }
         
         /* 格式化时间  */
         function dateFormat(time){
             var commentTime = time;
             var timeList = commentTime.toString().split(" ").slice(1,5);
             return timeList[2]+" "+timeList[1]+" "+timeList[0]+" "+timeList[3];
         }
         
         /*渲染orderList*/
         function _render_order(orderTemplate,data){
         	 var orderTemplateTemp = orderTemplate;
         	 orderTemplateTemp.empty();
             data.forEach(function(item){
            	 console.log(item);
                 var clone = $(".order-sample").clone().removeClass("hidden-attr").removeClass("order-sample");
                 var childOdClone = clone.children("#order-details-list");
                 /*date*/
                 childOdClone.children("div:first").children("h3").text();
                 /*time*/
                 var  newDate = new Date();
                 newDate.setTime(item.createDate*1000);
                 childOdClone.children("div:first").children("p").text(newDate.toDateString());
                 /*customer name*/
                 childOdClone.children("div:nth-child(2)").children("h3").text(item.customer.name);
                 /*customer address*/
                 childOdClone.children("div:nth-child(2)").children("p").text(item.deliveryAddress);
                 /*Money Total*/
                 var total = 0;
                 item.orderItemList.forEach(function(item){
                 	total = total + item.price*item.amount;
                 });
                 childOdClone.children("div:nth-child(3)").children("h3").text("Total");
                 /*Proess Number*/
                 childOdClone.children("div:nth-child(3)").children("p").text(total);
                 /*Order Statu*/
                 childOdClone.children("div:nth-child(4)").children("h3").text("Status");
                 childOdClone.children("div:nth-child(4)").children("p").text(item.status).attr("id","status"+item.id);
                 /* order processnumber*/
                 childOdClone.children("div:nth-child(5)").children("h3").text("processNumber");
                 childOdClone.children("div:nth-child(5)").children("p").text(item.processNumber);
                 /* reply button*/
                 var replyBtn =  childOdClone.children("div:nth-child(6)").children("button:first");
                 replyBtn.attr("id","detailBtn")
                     .attr("value", item.id);
                 var comment_flag = 0;
                 var customer_comment = null;
                 item.comment.forEach(function(comment){
                	 if(comment_flag == 0){
                		 customer_comment = comment;
                		 console.log( customer_comment);
                	 }
                	 comment_flag++;
                 });
                 if(comment_flag != 1){
                     replyBtn.attr("disabled", "disabled");
                 }
                 replyBtn.on("click", function(){
                	 console.log( customer_comment);
                     $("#customer-name").attr("value", item.id).text("Reply To: " +item.customer.name);
                     $("#customer-comment-+time").text(dateFormat(customer_comment.comentDate));
                     /*添加customer-comment*/
                     $("#customer-comment").text(customer_comment.content ); 
                 });
                 /* add to dom tree */
                 console.log(clone);
                 orderTemplateTemp.append(clone);
             });
         }
     };
     $(function(){
         var orderShow = new orderListShow($("#show-order-area"));
         orderShow.findOrderByStatus(); 
         var ret = /^[\u4E00-\u9FA.]{1,50}$/;
         $("#reply-btn").on("click", function(){
             var o_id = $("#customer-name").attr("value");
             var comment_reply = $("#customer-comment-text").val();
            /*  输入信息验证 */
             if(ret.test(comment_reply)){
                     /*发送请求*/
            	 ajaxRequest.ajax("POST","/spring-merchant/replyComment?comment="+comment_reply+"&o_id="+o_id,null,null,this,function(feedback){
 		    	 		if("success" == feedback)
 		    	 			window.location.reload();
 		    	 		else
 		    	 			$(".alert-failed").removeClass("hidden-attr"); 
            	 });
             }
             else{
                 $(".alert-unqualified").removeClass("hidden-attr");   
             }
         });
     });